package 链表.复杂链表的复制;

import 链表.链表逆置.ListNode;

import java.util.HashMap;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/2 16:51
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {

    }

    //TODO 官方推荐解法
    public static Node copyRandomList(Node head) {
        if(head == null){
            return null;
        }
        Node cur = head;
        HashMap<Node,Node> map = new HashMap<>();
        while(cur!=null){
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur=head;
        while(cur!=null){
            map.get(cur).next=map.get(cur.next);
            map.get(cur).random=map.get(cur.random);
            cur=cur.next;
        }
        return map.get(head);
    }
}
